package xyz.hzxiao.utils;

import java.security.SecureRandom;
import java.util.Random;

/**
 * @Author: scy
 * @Date: 2022-09-29 11:12
 */
public class RandomUtil {

    private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static final Random RANDOM = new SecureRandom();

    /**
     * 获取随机字符串 Nonce Str
     *
     * @return String 随机字符串
     */
    public static String generateNonceStr() {
        char[] nonceChars = new char[8];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        String timestamp = String.valueOf(System.currentTimeMillis());
        return new String(nonceChars) + timestamp.substring(timestamp.length() - 4);
    }
}
